/*
 * Tester.cpp
 *
 *  Created on: Apr 4, 2012
 *      Author: ajeffery
 */

#include "Tester.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

Tester::Tester() {
	total_tests= 0;
	tests_passed= 0;
	tests_failed= 0;
	tests= NULL;
}

Tester::~Tester() {
	test_list_t *trace;
	while (tests != NULL){
		trace = tests;
		tests = (test_list_t*) tests->next;
		free(trace);
	}
}

void Tester::register_test(int (*fd)(), char* class_name, char* functionality){
	test_list_t *new_test;
	new_test = (test_list_t*) malloc(sizeof(test_list_t));
	if (new_test<=0){
		printf("Failed to Register Test :: malloc failed \n");
		return;
	}
	memset(new_test->func,0,BUF_SIZE);
	memset(new_test->name,0,BUF_SIZE);

	new_test->test_fd = fd;
	memcpy(new_test->name , class_name, strlen(class_name));
	memcpy(new_test->func , functionality, strlen(functionality));

	new_test->next = tests;
	tests = new_test;
	total_tests++;
}

void Tester::run_tests(){
	test_list_t *trace;
	trace =tests;
	for (int i = 0; i< total_tests; i++){
		int ret = (trace->test_fd)();
		print_results(ret, trace->name, trace->func);
		if (ret > 0)
			tests_passed++;
		else
			tests_failed++;

		trace = (test_list_t*)trace->next;
	}
	printf("\n ---- DONE ALL TESTS ---- \n");
	printf("Tests Passed: %d, Tests Failed %d, Total Tests %d \n",
			tests_passed, tests_failed, total_tests);
}

void Tester::print_results(int result, char* class_name, char* functionality){
	printf("Testing %10s :: %35s ..... ",class_name, functionality);
	if (result > 0)
		printf("Passed \n");
	else
		printf("FAILED \n");
}
